package cn.iocoder.yudao.module.distm.service.loss;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.distm.controller.admin.loss.vo.*;
import cn.iocoder.yudao.module.distm.dal.dataobject.loss.MealLossRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.distm.dal.mysql.loss.MealLossRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.distm.enums.ErrorCodeConstants.*;

/**
 * 配送管理-餐食损耗记录 Service 实现类
 *
 * @author yisla
 */
@Service
@Validated
public class MealLossRecordServiceImpl implements MealLossRecordService {

    @Resource
    private MealLossRecordMapper mealLossRecordMapper;

    @Override
    public Long createMealLossRecord(MealLossRecordSaveReqVO createReqVO) {
        // 插入
        MealLossRecordDO mealLossRecord = BeanUtils.toBean(createReqVO, MealLossRecordDO.class);
        mealLossRecordMapper.insert(mealLossRecord);
        // 返回
        return mealLossRecord.getId();
    }

    @Override
    public void updateMealLossRecord(MealLossRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateMealLossRecordExists(updateReqVO.getId());
        // 更新
        MealLossRecordDO updateObj = BeanUtils.toBean(updateReqVO, MealLossRecordDO.class);
        mealLossRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteMealLossRecord(Long id) {
        // 校验存在
        validateMealLossRecordExists(id);
        // 删除
        mealLossRecordMapper.deleteById(id);
    }

    @Override
        public void deleteMealLossRecordListByIds(List<Long> ids) {
        // 校验存在
        validateMealLossRecordExists(ids);
        // 删除
        mealLossRecordMapper.deleteByIds(ids);
        }

    private void validateMealLossRecordExists(List<Long> ids) {
        List<MealLossRecordDO> list = mealLossRecordMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(MEAL_LOSS_RECORD_NOT_EXISTS);
        }
    }

    private void validateMealLossRecordExists(Long id) {
        if (mealLossRecordMapper.selectById(id) == null) {
            throw exception(MEAL_LOSS_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public MealLossRecordDO getMealLossRecord(Long id) {
        return mealLossRecordMapper.selectById(id);
    }

    @Override
    public PageResult<MealLossRecordDO> getMealLossRecordPage(MealLossRecordPageReqVO pageReqVO) {
        return mealLossRecordMapper.selectPage(pageReqVO);
    }

}